#include <iostream>
#include <vector>
#include <map>
#include <string.h>
#define int long long
using namespace std;


struct xy{
	int x,y;
	int operator + (const xy a)const{
		return ((x-a.x)*(x-a.x)+(y-a.y)*(y-a.y));
	}
};


signed main(){
	int n,m;
	cin>>n>>m;
	int mx[n][m];
	memset(mx,0,sizeof(mx));
	map<int,int> mp;
	vector<vector<xy>> v;
	int index=1;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			int l;
			cin>>l;
			if(mp[l] == 0){
				mp[l] = index++;
				vector<xy> vec;
				vec.push_back({i,j});
				v.push_back(vec);
			}
			else{
//				cout<<"index:"<<i<<" "<<j<<endl;
				v[mp[l]-1].push_back({i,j});
			}
		}
	}
	for(int i=0;i<v.size();i++){
		for(int j=0;j<v[i].size();j++){
			for(int k=j+1;k<v[i].size();k++){
				mx[v[i][j].x][v[i][j].y] = max(mx[v[i][j].x][v[i][j].y],v[i][j]+v[i][k]);
				mx[v[i][k].x][v[i][k].y] = max(mx[v[i][k].x][v[i][k].y],v[i][j]+v[i][k]);
			}
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cout<<mx[i][j];
			if(j != m-1)
				cout<<" ";
		}
        if(i != n-1)
		    cout<<endl;
	}
	return 0;
}
